package com.moco.launcher.http;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import org.apache.http.NameValuePair;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BinaryHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.io.UnsupportedEncodingException;

public class HttpUtil {
	// 缃戠粶杩炴帴閮ㄥ垎

	public static String postByHttpURLConnection(String strUrl,
			NameValuePair... nameValuePairs) {
		return CustomHttpURLConnection.PostFromWebByHttpURLConnection(strUrl,
				nameValuePairs);
	}

	public static String getByHttpURLConnection(String strUrl,
			NameValuePair... nameValuePairs) {
		return CustomHttpURLConnection.GetFromWebByHttpUrlConnection(strUrl,
				nameValuePairs);
	}

	public static String postByHttpClient(Context context, String strUrl,
			NameValuePair... nameValuePairs) throws Exception {
		String result = CustomHttpClient.PostFromWebByHttpClient(context,
				strUrl, nameValuePairs);
		return result;
	}

	public static String getByHttpClient(Context context, String strUrl,
			NameValuePair... nameValuePairs) throws Exception {
		String result = CustomHttpClient.getFromWebByHttpClient(context,
				strUrl, nameValuePairs);

		if (TextUtils.isEmpty(result)) {
			result = "";
		}

		return result;
	}

	// 缃戠粶杩炴帴鍒ゆ柇
	/**
	 * 鍒ゆ柇鏄惁鏈夌綉锟�
	 */
	public static boolean isNetworkAvailable(Context context) {
		return NetWorkHelper.isNetworkAvailable(context);
	}

	/**
	 * 鍒ゆ柇mobile缃戠粶鏄惁鍙敤
	 */
	public static boolean isMobileDataEnable(Context context) {
		String TAG = "httpUtils.isMobileDataEnable()";
		try {
			return NetWorkHelper.isMobileDataEnable(context);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 鍒ゆ柇wifi缃戠粶鏄惁鍙敤
	 */
	public static boolean isWifiDataEnable(Context context) {
		String TAG = "httpUtils.isWifiDataEnable()";
		try {
			return NetWorkHelper.isWifiDataEnable(context);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 鍒ゆ柇鏄惁涓烘极锟�
	 */
	public static boolean isNetworkRoaming(Context context) {
		return NetWorkHelper.isNetworkRoaming(context);
	}

	/**
	 * 缂栫爜娴嬭瘯
	 */
	public static void testCharset(String datastr) {
		try {
			String temp = new String(datastr.getBytes(), "GBK");
			Log.v("TestCharset", "****** getBytes() -> GBK ******/n" + temp);
			temp = new String(datastr.getBytes("GBK"), "UTF-8");
			Log.v("TestCharset", "****** GBK -> UTF-8 *******/n" + temp);
			temp = new String(datastr.getBytes("GBK"), "ISO-8859-1");
			Log.v("TestCharset", "****** GBK -> ISO-8859-1 *******/n" + temp);
			temp = new String(datastr.getBytes("ISO-8859-1"), "UTF-8");
			Log.v("TestCharset", "****** ISO-8859-1 -> UTF-8 *******/n" + temp);
			temp = new String(datastr.getBytes("ISO-8859-1"), "GBK");
			Log.v("TestCharset", "****** ISO-8859-1 -> GBK *******/n" + temp);
			temp = new String(datastr.getBytes("UTF-8"), "GBK");
			Log.v("TestCharset", "****** UTF-8 -> GBK *******/n" + temp);
			temp = new String(datastr.getBytes("UTF-8"), "ISO-8859-1");
			Log.v("TestCharset", "****** UTF-8 -> ISO-8859-1 *******/n" + temp);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	private static AsyncHttpClient client = new AsyncHttpClient(); // 瀹炰緥璇濆璞�
	static {

		client.setTimeout(10000); // 璁剧疆閾炬帴瓒呮椂锛屽鏋滀笉璁剧疆锛岄粯璁や负10s
	}

	// 鐢ㄤ竴涓畬鏁磚rl鑾峰彇涓�釜string瀵硅薄
	public static void get(String urlString, AsyncHttpResponseHandler res) {
		client.get(urlString, res);
	}

	// url閲岄潰甯﹀弬鏁�
	public static void get(String urlString, RequestParams params,
			AsyncHttpResponseHandler res) {
		client.get(urlString, params, res);
	}

	// 涓嶅甫鍙傛暟锛岃幏鍙杍son瀵硅薄鎴栬�鏁扮粍
	public static void get(String urlString, JsonHttpResponseHandler res) {
		client.get(urlString, res);
	}

	// 甯﹀弬鏁帮紝鑾峰彇json瀵硅薄鎴栬�鏁扮粍
	public static void get(String urlString, RequestParams params,
			JsonHttpResponseHandler res) {
		client.get(urlString, params, res);
	}

	// 涓嬭浇鏁版嵁浣跨敤锛屼細杩斿洖byte鏁版嵁
	public static void get(String uString, BinaryHttpResponseHandler bHandler) {
		client.get(uString, bHandler);
	}

	public static void post(String urlString, AsyncHttpResponseHandler handler) {
		client.post(urlString, handler);
	}

	public static AsyncHttpClient getClient() {

		return client;
	}
}
